//init
function init_admin()
{
	//
	$("[admin_type=text]")
		.bind('mouseover',text_mouseover)
		.bind('mouseout',text_mouseout)
		.each(function()
		{
			if ($(this).html().match(/^\s*$/)) $(this).html('&nbsp;');
		});
	
	$("[admin_type=button_item_list]")
		.addClass('admin_button')
		.html("{NEW_ITEM_BUTTON}");
		
	window.edit_input = false;
}


function unadmin()
{
	$("[admin_type=text]").unbind();
	$('[admin]').html('');
	$("[admin_type=button_item_list]").html('');
}


if($("#admin_option_div").length <= 0)
{

	var span1 = document.createElement('span');
	$(span1)
		.html("{EDIT}")
		.attr('class','option_button')
		.attr('action','edit')
		.bind('click',function()
		{
			$('[current_edit=yes]').trigger('dblclick');
		});

	var span2 = document.createElement('span');
	$(span2)
		.html("{SAVE}")
		.attr('class','option_button')
		.css('display','none')
		.attr('action','save')
		.click(function()
		{
			text_save();
		});

	var span3 = document.createElement('span');
	$(span3)
		.html("{CANCEL}")
		.attr('class','option_button')
		.css('display','none')
		.attr('action','cancel')
		.bind('click',text_cancel_click);
	

	var div = document.createElement('div');
	$(div)
		.attr('class','option_div')
		.attr('id','admin_option_div')
		.css('display','none')
		.append(span1)
		.append(span2)
		.append(span3)
		.bind('mouseover',function()
		{
			$(this)
				.stop()
				.fadeTo('fast',1);
		})
		.bind('mouseout',function()
		{
			$(this).fadeOut(100);
		});
	$(document.body).append(div);
}


function get_option_div()
{
	$('[action=edit]').fadeIn(100);
	$('[action=save]').css('display','none');
	$('[action=cancel]').css('display','none');
	var o = document.getElementById("admin_option_div");
	return o;
}


function text_mouseover(event,o)
{
	if (window.edit_input) return;
	var o = o?o:this;
	$(o)
		.addClass('text_mouseover')
		.attr('current_edit','yes')
		.bind('dblclick',text_edit);

	if ($(o).attr('admin_trigger') == 'click')
	{
		$(o).bind('click',text_edit);
	}
	
	if ($(o).attr('admin_button') != 'no')
	{
		var offset = $(o).offset();
		window.option_div_timer = setTimeout(
			"$(get_option_div())"
			+".css('left','"+(offset.left+$(o).width())+"px')"
			+".css('top','"+(parseInt(offset.top))+"px')"
			+".fadeIn(100);",500);
	}

}



function remove_current_edit()
{
	$('[current_edit=yes]').removeAttr('current_edit');
}


function text_mouseout(event,o)
{
	if (window.option_div_timer)
	{
		clearTimeout(window.option_div_timer);
	}
	if (window.edit_input) return;
	var o = o?o:this;
	if ($(o).attr('admin_button') != 'no')
		$(get_option_div()).fadeOut(300,remove_current_edit);
	$(o).removeClass('text_mouseover');
}

function text_cancel_click(event,o)
{
	var o = o?o:this;
	if (window.text_save_timer) clearTimeout(window.text_save_timer);
	$('[edit_input=yes]').css('display','none'); //隐藏输入框
	var current_edit = $('[editing=yes]');
	current_edit
		.css('display','none')
		.html(current_edit.attr('old_value'))
		.fadeIn('fast')
		.unbind()
		.bind('mouseover',text_mouseover)
		.bind('mouseout',text_mouseout);
	$('[edit_input=yes]').removeAttr('edit_input');
	window.edit_input = false;
	$('[action=cancel]').add('[action=save]').fadeOut(200);
	//$('[action=edit]').css('display','none');
	$(get_option_div())
		.bind('mouseout',function()
		{
			$(this).fadeOut(100);
		});
}


function text_edit(o)
{
	if ($('[edit_input=yes]').length) return;
	if (window.text_save_timer) clearTimeout(window.text_save_timer);

	$('[editing=yes]').removeAttr('editing');
	$('[edit_input=yes]').removeAttr('edit_input');
	window.edit_input = false;
	$('[saving=yes]').removeAttr('saving');
	if ($(this).attr('admin_select_value')) //如果是select类型
	{
		select_edit.call(this);
		return;
	}
	var val = $(this).html();
	var height = $(this).height()-2;
	var width = $(this).width()-2;
	if (width<50 ) width = 50;
	$(this)
		.trigger('mouseout')
		.attr('old_value',val)
		.attr('editing','yes')
		.unbind()
		.css('display','none');
	var input = document.createElement('input');
	$(input)
		.height(height)
		.css('text-align',$(this).css('text-align'))
		.width(width)
		.bind('keypress',function(event)
		{
			if (event.keyCode == 13)
			{
				window.text_save_timer = setTimeout("text_save()",10);
			}
		});
	input.value = (val == '&nbsp;')?'':val;

	$(this).parent().append(input);
	
	window.edit_input = true;
	$(input)
		.focus()
		.addClass('text_input')
		.attr('edit_input','yes')
		.bind('blur',function()
		{
			window.text_save_timer = setTimeout("text_save()",$('[editing=yes]').attr('admin_button') != 'no' ?500:100); //失去焦点后自动保存
		})
		.bind('keypress',function(event)
		{
			if (event.keyCode == 13)
			{
				window.text_save_timer = setTimeout("text_save()",10);
			}
		});
	var offset = $(input).offset();
	if ($('[editing=yes]').attr('admin_button') != 'no')
	{
		$(get_option_div())
			.unbind('mouseout')
			.css('left',offset.left + $(input).width()+3)
			.css('top',offset.top)
			.fadeIn(100);
		$('[action=edit]').css('display','none');
		$('[action=save]').add('[action=cancel]').fadeIn(100);
	}
}

//select 
function select_edit(o)
{
	var val = $(this).attr('admin_select_value');
	var height = $(this).height()-2;
	var width = $(this).width()-2;
	if (width<50 ) width = 50;
	var source = $_($(this).attr('admin_select_source_id'));
	$(this)
		.trigger('mouseout')
		.attr('old_value',val)
		.attr('editing','yes')
		.unbind()
		.css('display','none');
	var input = document.createElement('select');
	for(i=0;i<source.childNodes.length;i++)
	{
		var o = source.childNodes[i].cloneNode(true);
		if (o.value == val)
		{
			$(o).attr('selected','selected');
		}
		else
		{
			$(o).removeAttr('selected');
		}
		$(input).append(o);
	}
	$(input)
		.height(height)
		.width(width);


	$(this).parent().append(input);
		
	$(input)
		.addClass('text_input')
		.attr('edit_input','yes')
		.focus()
		.bind('blur',function()
		{
			window.text_save_timer = setTimeout("text_save()",$('[editing=yes]').attr('admin_button') != 'no' ?500:100); //失去焦点后自动保存
		})
		.bind('change',function(event)
		{
			window.text_save_timer = setTimeout("text_save()",100);
		});
	var offset = $(input).offset();

}

function text_save()
{
	var val = $('[edit_input=yes]').val();
	if (!val) return;
	var p = $('[editing=yes]');
	
	var para = p.attr('admin_para');
	p.html('{SAVING_HTML}')
		.attr('saving','yes')
		.fadeIn('fast');
	$('#admin_option_div').css('display','none');
	$('[edit_input=yes]')
		.removeAttr('edit_input')
		.css('display','none');
	$('[edit_input=yes]').removeAttr('edit_input');
	$('[editing=yes]').removeAttr('editing');
	
	if (p.attr('admin_select_value'))
	{
		p.attr('admin_select_value',val);
	}
	
	if (p.attr('old_value') != val)
		$.post('admin.php?p=ajax&'+para,{'value':val},text_ajax_callback);
	else
		text_ajax_callback(val);
}

function text_ajax_callback(s)
{
	var o = $('[saving=yes]');
	$('[edit_input=yes]').stop().css('display','none');
	if (o.attr('admin_select_value'))
	{
		var source = $_(o.attr('admin_select_source_id'));
		for(i=0;i<source.childNodes.length;i++)
		{
			if ($(source.childNodes[i]).val() == s)
			{
				s = $(source.childNodes[i]).html();
			}
		}
	}
	window.edit_input = false;
	o.html(s)
		.removeAttr('saving')
		.fadeIn('fast');
	init_admin(); 
}


function run_logout()
{
	$.post('login.php?module=ajax&m=logout',{},logout_callback,'json');
}

function logout_callback(data)
{
	if (data.msg) 
		alert(data.msg);
	if (data.eval)
		try{ eval(data.eval); } catch(e) {  }
	if (data.success)
	{
		unadmin();
		$('[id^=footer_]').css('display','none');
		$('#footer_login').fadeIn();
	}
}


init_admin();